package com.rr.easy;

/**
 * @author zhuangcanrun
 * @Date 2022/01/05/21:52
 * @description
 **/

public class ModifyString {
    public String modifyString(String s) {
        int length = s.length();
        if (length == 1) {
            return s.charAt(0) == '?' ? "a" : s;
        }
        StringBuilder builder = new StringBuilder();
        char preChar = 96;
        char middleChar = s.charAt(0);
        for (int i =1; i < length; i++) {
            char nextChar = s.charAt(i);
            if (middleChar == '?') {
                if (nextChar == '?') {
                    for (char tempChar = 'a'; tempChar <= 'z'; tempChar++) {
                        if (tempChar != preChar) {
                            builder.append(tempChar);
                            preChar = tempChar;
                            break;
                        }
                    }
                } else {
                    for (char tempChar = 'a'; tempChar <= 'z'; tempChar++) {
                        if (tempChar != preChar && tempChar != nextChar) {
                            builder.append(tempChar);
                            preChar = middleChar;
                            break;
                        }
                    }
                }
            } else {
                builder.append(middleChar);
                preChar = middleChar;
            }
            middleChar = nextChar;
        }
        if (middleChar != '?') {
            builder.append(middleChar);
        } else {
            for (char tempChar = 'a'; tempChar <= 'z'; tempChar++) {
                if (tempChar != preChar ) {
                    builder.append(tempChar);
                    break;
                }
            }
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        ModifyString modifyString = new ModifyString();
        System.out.println(modifyString.modifyString("j?qg??b"));
    }
}
